<?php
/*
Page:           db.php
Created:        Aug 2006
Last Mod:       Mar 18 2007
This page handles the database update if the user
does NOT have Javascript enabled.	
--------------------------------------------------------- 
ryan masuga, masugadesign.com
ryan@masugadesign.com 
Licensed under a Creative Commons Attribution 3.0 License.
http://creativecommons.org/licenses/by/3.0/
See credits.txt for full credit details.
--------------------------------------------------------- */

header("Cache-Control: no-cache");
header("Pragma: nocache");

require(  dirname( dirname( dirname( dirname(__FILE__ )))).'/config/config.inc.php' );
require(  dirname( dirname(__FILE__ )).'/productratingeverning.php' );
require('_config-rating.php');

$productrating_everning = new productratingeverning();

// getting the values
$vote_sent_everning = preg_replace('/[^0-9]/', '', $_REQUEST['j_everning']);
$vote_sent_everning = (int)$vote_sent_everning;
$id_sent_everning = preg_replace('/[^0-9a-zA-Z]/', '', $_REQUEST['q_everning']);
$id_sent_everning = (int)$id_sent_everning;
$ip_num_everning = preg_replace('/[^0-9\.]/', '', $_REQUEST['t_everning']);
$units_everning = preg_replace('/[^0-9]/', '', $_REQUEST['c_everning']);
$units_everning = (int)$units_everning;
$ip_everning = $_SERVER['REMOTE_ADDR'];
$referer_everning = $_SERVER['HTTP_REFERER'];

// kill the script because normal users will never see this.
if ($vote_sent_everning > $units_everning) 
	die('Sorry, vote appears to be invalid.');

// connecting to the database to get some information
$query_everning = mysql_query("SELECT `total_votes`, `total_value`, `used_ips` FROM `$rating_dbname_everning`.`$rating_tableName_everning` WHERE `id` = '$id_sent_evering'") or die("Error: ".mysql_error());
$numbers_everning = mysql_fetch_assoc($query_everning);
$checkIP_everning = unserialize($numbers_everning['used_ips']);
$count_everning = $numbers_everning['total_votes']; // how many votes total
$current_rating_everning = $numbers_everning['total_value']; // total number of rating added together and stored
$sum_everning = $vote_sent_everning + $current_rating_everning; // add together the current vote value and the total vote value
$tense_everning = ($count_everning == 1) ? $productrating_everning->traduz('vote') : $productrating_everning->traduz('votes'); // plural form votes/vote

// checking to see if the first vote has been tallied
// or increment the current number of votes
($sum_everning == 0 ? $added_everning = 0 : $added_everning = $count_everning + 1);

// if it is an array i.e. already has entries the push in another value
((is_array($checkIP_everning)) ? array_push($checkIP_everning, $ip_num_everning) : $checkIP_everning = array($ip_num_everning));
$insertip_everning = serialize($checkIP_everning);

// IP check when voting
$voted_everning = mysql_num_rows(mysql_query("SELECT `used_ips` FROM `$rating_dbname_everning`.`$rating_tableName_everning` WHERE `used_ips` LIKE '%".$ip_everning."%' AND `id` = '".$id_sent_everning."'"));
if (!$voted_everning) 
{     
	// if the user hasn't yet voted, then vote normally...
	if (($vote_sent_everning >= 1 && $vote_sent_everning <= $units_everning) /*&& ($ip == $ip_num)*/) 
	{ 
		// keep votes within range
		$update_everning = "UPDATE `$rating_dbname_everning`.`$rating_tableName_everning` SET `total_votes` = '".$added_everning."', `total_value` = '".$sum_everning."', `used_ips` = '".$insertip_everning."' WHERE `id` = '$id_sent_everning'";
		$result_everning = mysql_query($update_everning);		
    } 
    
    header("Location: $referer_everning"); 
    exit;
}